﻿using Sidvall.Collections;

namespace WebOptimizer.UI.Entities.Caching
{
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
	public partial class EntityCache : Sidvall.UI.Entities.EntityCacheBase
	{
		#region Public Members

		#region SourceCache

		private SourceCollection _SourceCache;
		public SourceCollection SourceCache
		{
			get
			{
				return _SourceCache;
			}
			private set
			{
				_SourceCache = value;
				_SourceIndex = null;
				OnPropertyChanged("SourceCache");
			}
		}

		#endregion
		#region TaskCache

		private TaskCollection _TaskCache;
		public TaskCollection TaskCache
		{
			get
			{
				return _TaskCache;
			}
			private set
			{
				_TaskCache = value;
				_TaskIndex = null;
				OnPropertyChanged("TaskCache");
			}
		}

		#endregion
		
		#region SourceIndex

		private System.Collections.Generic.Dictionary<int, Source> _SourceIndex;
		public System.Collections.Generic.Dictionary<int, Source> SourceIndex
		{
			get
			{
				if (_SourceIndex == null)
				{
					if (this.SourceCache != null)
						this.SourceIndex = this.SourceCache.ToDictionary<int, Source>(o => o.SourceId.Value, true);
				}
				return _SourceIndex;
			}
			private set
			{
				_SourceIndex = value;
				OnPropertyChanged("SourceIndex");
			}
		}

		#endregion
		#region TaskIndex

		private System.Collections.Generic.Dictionary<int, Task> _TaskIndex;
		public System.Collections.Generic.Dictionary<int, Task> TaskIndex
		{
			get
			{
				if (_TaskIndex == null)
				{
					if (this.TaskCache != null)
						this.TaskIndex = this.TaskCache.ToDictionary<int, Task>(o => o.TaskId.Value, true);
				}
				return _TaskIndex;
			}
			private set
			{
				_TaskIndex = value;
				OnPropertyChanged("TaskIndex");
			}
		}

		#endregion
		
		#region LoadSourceCache

		public void LoadSourceCache(SourceCollection item)
		{
			LoadSourceCache(item, false);
		}
		public void LoadSourceCache(SourceCollection item, bool refresh)
		{
			if ((this.SourceCache == null) || (refresh))
				this.SourceCache = item;
		}

		#endregion
		#region LoadTaskCache

		public void LoadTaskCache(TaskCollection item)
		{
			LoadTaskCache(item, false);
		}
		public void LoadTaskCache(TaskCollection item, bool refresh)
		{
			if ((this.TaskCache == null) || (refresh))
				this.TaskCache = item;
		}

		#endregion
	
		#region FindSource

		public Source FindSource(int sourceId)
		{
			Source item;

			if (this.SourceIndex == null)
				return null;
			if (this.SourceIndex.TryGetValue(sourceId, out item))
				return item;
			return null;
		}

		#endregion
		#region FindTask

		public Task FindTask(int taskId)
		{
			Task item;

			if (this.TaskIndex == null)
				return null;
			if (this.TaskIndex.TryGetValue(taskId, out item))
				return item;
			return null;
		}

		#endregion
		
		#endregion
		#region Constructors

		public EntityCache()
		{
		}

		#endregion
	}
}